package org.cainiao.jvm.lambda;

import java.lang.invoke.*;

/**
 * <br />
 * <p>
 * Author: Cai Niao(wdhlzd@163.com)<br />
 */
public class DynamicInvokeExample {

    public static void main(String[] args) throws Throwable {
        CallSite callSite = bootstrap();

        MethodHandle methodHandle = callSite.getTarget();

        int result = (int) methodHandle.invokeExact(5, 3);
    }

    public static CallSite bootstrap() throws Throwable {
        MethodHandles.Lookup lookup = MethodHandles.lookup();
        MethodHandle targetMethod = lookup.findStatic(DynamicInvokeExample.class, "add",
            MethodType.methodType(int.class, int.class, int.class));
        return new ConstantCallSite(targetMethod);
    }

    public static int add(int a, int b) {
        return a + b;
    }
}
